import { initConfig, initSSRApp } from '@keenoho/site-builder';
import { renderToString } from 'vue/server-renderer';
import KeenohoUI from '@keenoho/keenoho-ui/node';
import App from './App.vue';

// init

// start to run
async function start() {
  // prepare setting
  const config = initConfig();

  const beforeAppMount = async (app) => {
    app.config.globalProperties.$config = config;
    app.use(KeenohoUI);
  };

  // render app
  const app = await initSSRApp({}, App, beforeAppMount);
  return app;
}

export default async function render(url, ctx) {
  const app = await start();
  const appHtml = await renderToString(app, ctx);
  return appHtml;
}
